package com.lunwen.view;

import com.lunwen.dao.ArticleDao;
import com.lunwen.domain.Article;
import com.lunwen.utils.StringUtils;
import com.lunwen.view.component.Context;
import com.lunwen.view.component.MassageTip;
import com.lunwen.view.value.ColorValue;
import com.lunwen.view.value.SizeValue;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ArticleFormFrame extends Context implements ActionListener {


    private JTextField sid = new JTextField(10);
    private JTextField article_name = new JTextField(10);
    private JTextField article_type = new JTextField(10);
    private JPanel article_content = new JPanel();
    private JTextArea content = new JTextArea(13,42);

    private JButton add = new JButton("确认新增");

    private ArticleDao dao = new ArticleDao();

    public ArticleFormFrame(int width, int height) {
        super(width, height);
        sid.setBounds(60,0,width-120,48);
        article_name.setBounds(60,50,width-120,48);
        article_type.setBounds(60,100,width-120,48);
        article_content.setBounds(60,150,width-120,250);
        this.add(sid,"作者学号",100);
        this.add(article_name,"论文题目",100);
        this.add(article_type,"论文类型",100);

        JLabel label = new JLabel("论文的详细内容");
        label.setBounds(0,0,width-120,48);
        label.setFont(new Font("幼圆", Font.BOLD, SizeValue.ContextFont));
        label.setForeground(ColorValue.ContextForeground);
        article_content.add(label);

        content.setLineWrap(true);
        content.setEditable(true);

        JScrollPane scrollpane=new JScrollPane(content);
        scrollpane.setBounds(5,48,470,195);
        article_content.add(scrollpane);
        content.setForeground(ColorValue.ContextForeground);
        article_content.setBackground(ColorValue.ContextBackground);
        article_content.setForeground(Color.GRAY);
        article_content.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, ColorValue.Border));

        this.add(article_content);

        this.setVisible(true);
        add.setBounds(60,400,width-120,48);
        this.add(add,20);
        add.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == add){
            if (!StringUtils.isEmpty(sid.getText())){
                if (!StringUtils.isEmpty(article_name.getText())){
                    if (!StringUtils.isEmpty(new String(article_type.getText()))){
                        //新增文章
                        Article article = new Article();
                        article.setName(article_name.getText());
                        article.setType(article_type.getText());
                        int aid = dao.insert(article);
                        article.setAid(aid);
                        //新增内容
                        article.setContent(content.getText());
                        dao.insertContent(article);
                        //新增学生关联
                        dao.insertRelation(aid,sid.getText());
                        MassageTip.showMassage(null,"添加成功");
                    }else{
                        MassageTip.showMassage(null,"类型不能为空");
                    }

                }else{
                    MassageTip.showMassage(null,"名称不能为空");
                }

            }else {
                MassageTip.showMassage(null,"学号不能为空");
            }

        }
    }
}